package com.itheima.service.store.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CourseDao;
import com.itheima.domain.store.Course;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.CourseService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class CourseServiceImpl implements CourseService {

    @Override
    public void save(Course course) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            CourseDao courseDao=MapperFactory.getMapper(sqlSession,CourseDao.class);
            String id= UUID.randomUUID().toString();
            course.setId(id);
            courseDao.save(course);
            TransactionUtil.commit(sqlSession);
        }catch(Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delete(Course course) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            CourseDao courseDao=MapperFactory.getMapper(sqlSession,CourseDao.class);
            courseDao.delete(course);
            TransactionUtil.commit(sqlSession);
        }catch(Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Course course) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            CourseDao courseDao=MapperFactory.getMapper(sqlSession,CourseDao.class);
            courseDao.update(course);
            TransactionUtil.commit(sqlSession);
        }catch(Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Course findById(String id) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            CourseDao courseDao=MapperFactory.getMapper(sqlSession,CourseDao.class);
            return courseDao.findById(id);
        }catch(Exception e) {
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Course> findAll() {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            CourseDao courseDao=MapperFactory.getMapper(sqlSession,CourseDao.class);
            return courseDao.findAll();
        }catch(Exception e) {
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo<Course> findAll(int page, int size) {
        SqlSession sqlSession=null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            CourseDao courseDao=MapperFactory.getMapper(sqlSession,CourseDao.class);
            PageHelper.startPage(page,size);
            List<Course>all=courseDao.findAll();
            PageInfo pageInfo=new PageInfo(all);
            return pageInfo;
        }catch(Exception e) {
            throw new RuntimeException(e);
        }finally{
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
